﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Painter.Model
{
    public static class VisualExtentsion
    {
        public static void FromImageSource(this DrawingVisual visual, ImageSource image)
        {
            using (DrawingContext dc = visual.RenderOpen())
                dc.DrawImage(image, new System.Windows.Rect(0, 0, image.Width, image.Height));
        }
        public static ImageSource ToImageSource(this DrawingVisual visual)
        {
            if (visual.DescendantBounds.IsEmpty) return null;
            RenderTargetBitmap render = new RenderTargetBitmap(
                (int)visual.DescendantBounds.Width,
                (int)visual.DescendantBounds.Height,
                0, 0, PixelFormats.Default
            );
            render.Render(visual);
            return render;
        }
        public static void Copy(this DrawingVisual visual, params DrawingVisual[] drawing)
        {
            using (DrawingContext dc = visual.RenderOpen())
            {
                foreach (DrawingVisual draw in drawing)
                {
                    dc.PushClip(draw?.Clip);
                    dc.PushTransform(draw?.Transform);
                    dc.DrawDrawing(draw.Drawing);
                    dc.Pop();
                    dc.Pop();
                }
            }
        }
    }
}
